package com.vito.dcs.asphalt.device.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vito.dcs.asphalt.device.dao.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.vito.base.factory.IDFactory;
import com.vito.base.model.DataGrid;
import com.vito.base.util.Validata;
import com.vito.dcs.asphalt.device.dao.MixAsphaltBlenderMapper;
import com.vito.dcs.asphalt.device.model.MixAsphaltBlenderModel;
import com.vito.dcs.asphalt.device.service.MixAsphaltBlenderService;
import com.vito.dcs.asphalt.pore.dao.MixAsphaltPoreMapper;
import com.vito.dcs.asphalt.pore.model.MixAsphaltPoreModel;

/**
 * MixAsphaltBlenderService接口实现类
 * @author  lyhao E-mail: 
 * @version 0.0.1
 * @date    2019-4-15 11:01:21
 */
@Service
public class MixAsphaltBlenderServiceImpl implements MixAsphaltBlenderService {
	
	@Autowired
	private MixAsphaltBlenderMapper mixasphaltblenderMapper;
	@Autowired
	private MixAsphaltPoreMapper mixAsphaltPoreMapper;
	
	@Override
	public void saveMixAsphaltBlender(Map<String,Object> map){
		map.put("id", IDFactory.getIDFactory().getIdInc()+"");
		//处理沥青form表单
		//MapUtil.mapAsphaltFrom(map);
		int blenderRes = mixasphaltblenderMapper.saveMixAsphaltBlender(map);
        if(blenderRes > 0){
            String poreParam = map.get("arrPoreJson").toString();
            if(!Validata.isNullOrEmpty(poreParam)){
                List<Map<String,Object>> list = (List<Map<String,Object>>)JSONArray.parse(poreParam);
                for (Map<String,Object> m : list) {
                    m.put("id",IDFactory.getIDFactory().getIdInc()+"");
                    m.put("blenderId", map.get("id"));
					//处理沥青table框
					//MapUtil.mapAsphaltTable(m);
                    mixAsphaltPoreMapper.saveMixAsphaltPore(m);
                }
            }
        }
	}
	@Override
	public DataGrid<MixAsphaltBlenderModel> queryMixAsphaltBlenderByPage(MixAsphaltBlenderModel mixasphaltblender){
		DataGrid<MixAsphaltBlenderModel> dg = new DataGrid<MixAsphaltBlenderModel>();
		List<MixAsphaltBlenderModel> blenderModelList = mixasphaltblenderMapper.queryMixAsphaltBlenderByPage(mixasphaltblender);
			MixAsphaltPoreModel pore = new MixAsphaltPoreModel();
		for (MixAsphaltBlenderModel blenderModel : blenderModelList) {
			pore.setBlenderId(blenderModel.getId());
			blenderModel.setPoreList(mixAsphaltPoreMapper.queryMixAsphaltPoreByPage(pore));
		}
		dg.setRows(blenderModelList);
		dg.setTotal(mixasphaltblender.getTotal());
		return dg;
	}
	@Override
	public MixAsphaltBlenderModel queryMixAsphaltBlender(MixAsphaltBlenderModel mixasphaltblender){
		return mixasphaltblenderMapper.queryMixAsphaltBlender(mixasphaltblender);
	}
	@Override
	public void updateMixAsphaltBlender(Map<String,Object> map){
		//处理沥青form表单
		//MapUtil.mapAsphaltFrom(map);
		if(mixasphaltblenderMapper.updateMixAsphaltBlender(map)>0){
			//处理数据
			String poreParam = map.get("arrPoreJson").toString();
            if(!Validata.isNullOrEmpty(poreParam)){
                List<Map<String,Object>> list = (List<Map<String,Object>>)JSONArray.parse(poreParam);
                for (Map<String,Object> m : list) {
                	//处理沥青table框
				//	MapUtil.mapAsphaltTable(m);
                    m.put("blenderId", map.get("id"));
                    mixAsphaltPoreMapper.updateMixAsphaltPore(m);
                }
            }
		}
	}
	
	@Override
	public void deleteMixAsphaltBlender(MixAsphaltBlenderModel mixasphaltblender){
		String id = mixasphaltblender.getId();
		mixasphaltblenderMapper.deleteMixAsphaltBlender(id);
		mixAsphaltPoreMapper.deleteMixAsphaltPore(id);
	}
	@Override
	public void updateIsCurrent(Map<String, Object> map) {
		/*String isCurrent=map.get("isCurrent").toString();
//		if("0".equals(isCurrent)){if("0".equals(isCurrent)){
			//mixasphaltblenderMapper.updateMixAsphaltBlender(map);
		*//*}else if("1".equals(isCurrent)){
			if(mixasphaltblenderMapper.updateMixAsphaltBlender(map)>0){
				mixasphaltblenderMapper.updateIsCurrent(map);
			}
		}*/
		if(mixasphaltblenderMapper.updateMixAsphaltBlender(map)>0){
			mixasphaltblenderMapper.updateIsCurrent(map);
		}
	}
	@Override
	public List<HashMap<String, String>> findBlenderBySectionId(Map<String, String> map) {
		return mixasphaltblenderMapper.findBlenderBySectionId(map);
	}

	@Override
	public String queryAsphaltLayerIdMachineNo(Map<String, String> map) {
		return mixasphaltblenderMapper.queryAsphaltLayerIdMachineNo(map);
	}

}